package com.duan.order.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class RabbitMqConfig {
    //队列
    public static final String ORDER_QUEUE = "order.queue";
    public static final String TASK_QUEUE = "task.queue";
    public static final String ERROR_QUEUE = "error.queue";
    public static final String CART_QUEUE = "cart.queue";



    //交换机
    public static final String ORDER_EXCHANGE="order.exchange";

    public static final String TASK_EXCHANGE = "task.exchange";


    //声明队列
    /*
     *   new Queue(QUEUE_EMAIL,true,false,false)
     *   durable="true" 持久化 rabbitmq重启的时候不需要创建新的队列
     *   auto-delete 表示消息队列没有在使用时将被自动删除 默认是false
     *   exclusive  表示该消息队列是否只在当前connection生效,默认是false
     */
    @Bean(ORDER_QUEUE)
    public Queue RED_QUEUE() {
        return new Queue(ORDER_QUEUE,true);
    }
    @Bean(TASK_QUEUE)
    public Queue TASK_QUEUE() {
        return new Queue(TASK_QUEUE,true);
    }
    @Bean(ERROR_QUEUE)
    public Queue ERROR_QUEUE() {
        return new Queue(ERROR_QUEUE,true);
    }
    @Bean(CART_QUEUE)
    public Queue CART_QUEUE() {
        return new Queue(CART_QUEUE,true);
    }

    @Bean(ORDER_EXCHANGE)
    public Exchange RED_EXCHANGE(){
        return ExchangeBuilder.directExchange(ORDER_EXCHANGE).durable(true).build();
    }

    //声明交换机
    @Bean(TASK_EXCHANGE)
    public Exchange TASK_EXCHANGE() {
        //durable(true) 持久化，mq重启之后交换机还在
        return ExchangeBuilder.fanoutExchange(TASK_EXCHANGE).durable(true).build();
    }


    @Bean
    public Binding Red_EXCHANGE_QUEUE(@Qualifier(ORDER_QUEUE)Queue queue, @Qualifier(ORDER_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(ORDER_QUEUE).noargs();
    }

//    @Bean
//    public Binding CART_EXCHANGE_QUEUE(@Qualifier(CART_QUEUE)Queue queue, @Qualifier(ORDER_EXCHANGE) Exchange exchange){
//        return BindingBuilder.bind(queue).to(exchange).with(CART_QUEUE).noargs();
//    }

    //队列绑定交换机，指定routingKey
    @Bean
    public Binding TASK_EXCHANGE_QUEUE(@Qualifier(TASK_QUEUE) Queue queue, @Qualifier(TASK_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(TASK_QUEUE).noargs();
    }



    @Bean
    public Binding ERROR_EXCHANGE_QUEUE(@Qualifier(ERROR_QUEUE)Queue queue, @Qualifier(ORDER_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(ERROR_QUEUE).noargs();
    }

}

